package com.example.hospitalmanagementapp.adapter;

import android.app.Person;
import android.content.Context;
import android.graphics.Color;
import android.opengl.Visibility;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;

import com.example.hospitalmanagementapp.R;
import com.example.hospitalmanagementapp.bean.SystemBean;

import java.util.Arrays;
import java.util.List;



/**
 * @author 席
 *左侧挂科系统adapter
 */
public class SystemAdapter extends RecyclerView.Adapter<SystemAdapter.SystemHolder>{


    private Context context;
    private List<SystemBean> systemStr;
    private ListenerRecyclerView listener;
    private int select;

    public SystemAdapter(Context context){
        this.context=context;
    }
    public void setSystemStr(List<SystemBean> systemStr){
        this.systemStr=systemStr;
    }
    public void setSystemListener(ListenerRecyclerView listener){
        this.listener=listener;
    }

    /**新建holder
     */

    @Override
    public SystemHolder onCreateViewHolder(@NonNull  ViewGroup parent, int viewType) {
        SystemHolder systemHolder=new SystemHolder(LayoutInflater.from(context).inflate(R.layout.item_genealogical_classify,null));
        return systemHolder;
    }

    /**绑定数据，建立数据和条目布局子控件之间的对应关系
     *判断是否被选中
     * @param holder
     * @param position
     */
    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void onBindViewHolder(@NonNull  SystemHolder holder, int position) {
        holder.systemName.setText(systemStr.get(position).getSystemName());

        if (select != position) {
            holder.itemView.setBackgroundColor(context.getColor(R.color.backcolor));
            holder.systemName.setTextColor(context.getColor(R.color.purple_700));
            holder.choseView.setVisibility(View.INVISIBLE);

        }
        else {
            holder.choseView.setVisibility(View.VISIBLE);
            holder.itemView.setBackgroundColor(context.getColor(R.color.white));
            holder.systemName.setTextColor(context.getColor(R.color.material_on_background_emphasis_medium));
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)  {

                if(null!=listener){
                    select=position;
                    //监听器引用
                    listener.onItemClick(position);
                    notifyDataSetChanged();
                }


            }
        });

    }

    @Override
    public int getItemCount() {
        return null==systemStr ? 0 :systemStr.size();
    }

    class  SystemHolder extends RecyclerView.ViewHolder {
        TextView systemName;
        View choseView;

        public SystemHolder(@NonNull  View itemView) {
            super(itemView);

            systemName=itemView.findViewById(R.id.item_recyclerview_tv);
            choseView=itemView.findViewById(R.id.item_recyclerview_view);

        }
    }

    /**
     自己新建一个接口，用来实现监听（从适配器到页面的传递）
     为什么要建在这个适配器的内部，因为新建在外面，是为了复用这个接口，此时不需要复用（像系统的View.OnClickListener()）
     */
    public interface ListenerRecyclerView {
        void onItemClick(int position);
    }

}
